package com.cxj.link.view;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.cxj.link.util.ImageUtil;
import com.cxj.link.vo.GameConfig;

public abstract class AbstractBoard {
    public Cut[][] create(GameConfig paramGameConf) {
        int k = paramGameConf.getXSize();
        int i = paramGameConf.getYSize();
        int[] arrayOfInt = new int[2];
        arrayOfInt[0] = k;
        arrayOfInt[1] = i;
        Cut[][] arrayOfPiece = (Cut[][]) Array.newInstance(Cut.class, arrayOfInt);
        List localList2 = createPieces(paramGameConf, arrayOfPiece);
        List localList1 = ImageUtil.getPlayImages(paramGameConf.getContext(), localList2.size());
        int m = ((CutImage) localList1.get(0)).getImage().getWidth();
        int n = ((CutImage) localList1.get(0)).getImage().getHeight();
        for (int j = 0; j < localList2.size(); j++) {
            Cut localPiece = (Cut) localList2.get(j);
            localPiece.setImage((CutImage) localList1.get(j));
            localPiece.setBeginX(m * localPiece.getIndexX() + paramGameConf.getBeginImageX());
            localPiece.setBeginY(n * localPiece.getIndexY() + paramGameConf.getBeginImageY());
            arrayOfPiece[localPiece.getIndexX()][localPiece.getIndexY()] = localPiece;
        }
        return arrayOfPiece;
    }

    protected List<Cut> createPieces(GameConfig paramGameConf, Cut[][] paramArrayOfPiece) {
        ArrayList localArrayList = new ArrayList();
        int[][] arrayOfInt = initPieces(paramGameConf);
        for (int j = 0; j < paramArrayOfPiece.length; j++)
            for (int i = 0; i < paramArrayOfPiece[j].length; i++) {
                if (arrayOfInt[j][i] == 0)
                    continue;
                localArrayList.add(new Cut(j, i));
            }
        return localArrayList;
    }

    protected abstract int[][] initPieces(GameConfig paramGameConf);

    public Cut[][] reCreatePieces(GameConfig paramGameConf, Cut[][] paramArrayOfPiece) {
        int[][] arrayOfInt = initPieces(paramGameConf);
        ArrayList localArrayList = new ArrayList();
        for (int i = 0; i < paramGameConf.getXSize() * paramGameConf.getYSize(); i++) {
            int xx = i / paramGameConf.getYSize();
            int j = i - xx * paramGameConf.getYSize();
            if (arrayOfInt[xx][j] != 0)
                localArrayList.add(Integer.valueOf(i));
        }
        Collections.shuffle(localArrayList);
        int n = 0;
        int[] arrayOfInt1 = new int[2];
        arrayOfInt1[0] = paramGameConf.getXSize();
        arrayOfInt1[1] = paramGameConf.getYSize();
        Cut[][] arrayOfPiece = (Cut[][]) Array.newInstance(Cut.class, arrayOfInt1);
        for (int k = 0; k < paramGameConf.getXSize() * paramGameConf.getYSize(); k++) {
            int m = k / paramGameConf.getYSize();
            int i = k - m * paramGameConf.getYSize();
            if (arrayOfInt[m][i] == 0)
                continue;
            int i1 = ((Integer) localArrayList.get(n)).intValue();
            n = i1 / paramGameConf.getYSize();
            i1 -= n * paramGameConf.getYSize();
            Cut localPiece2 = paramArrayOfPiece[n][i1];
            if ((localPiece2 != null) && (arrayOfPiece[m][i] == null)) {
                arrayOfPiece[m][i] = localPiece2;
                localPiece2.setIndexX(m);
                localPiece2.setIndexY(i);
                localPiece2.setBeginX(localPiece2.getIndexX() * localPiece2.getImage().getImage().getWidth()
                        + paramGameConf.getBeginImageX());
                localPiece2.setBeginY(localPiece2.getIndexY() * localPiece2.getImage().getImage().getHeight()
                        + paramGameConf.getBeginImageY());
            }
        }
        return arrayOfPiece;
    }
}
